layui.use(['element','form','laydate','upload'],function(){
    var laydate = layui.laydate,
        upload = layui.upload,
    form = layui.form;

    laydate.render({
        elem: '#registeredDate' //指定元素
    });


    //省选择回调
    form.on('select(province)', function(data){
        var name = $(data.elem).find("option:selected").text();
        $("#provinceName").val(name);
        $("#provinceCode").val(data.value);
        $.ajax({
            url:"/city?pId="+data.value,
            type:'get',
            dataType:'json',
            success:function (rs) {
                if(rs){
                    $("#city").empty().append('<option value="">请选择市</option>');
                    $.each(rs,function(i,d){
                        $("#city").append('<option value="'+d.id+'">'+d.name+'</option>');
                    });
                    form.render('select'); //刷新select选择框渲染
                }
            }
        });
    });

    //市选择回调
    form.on('select(city)', function(data){
        var name = $(data.elem).find("option:selected").text();
        $("#cityName").val(name);
        $("#cityCode").val(data.value);
        $.ajax({
            url:"/area?pId="+data.value,
            type:'get',
            dataType:'json',
            success:function (rs) {
                if(rs){
                    $("#area").empty().append('<option value="">请选择区(县)</option>');
                    $.each(rs,function(i,d){
                        $("#area").append('<option value="'+d.id+'">'+d.name+'</option>');
                    });
                    form.render('select'); //刷新select选择框渲染
                }
            }
        });
    });

    //区选择回调
    form.on('select(area)', function(data){
        var name = $(data.elem).find("option:selected").text();
        $("#areaName").val(name);
        $("#areaCode").val(data.value);
        $.ajax({
            url:"/town?pId="+data.value,
            type:'get',
            dataType:'json',
            success:function (rs) {
                if(rs){
                    $("#town").empty().append('<option value="">请选择乡镇(街道)</option>');
                    $.each(rs,function(i,d){
                        $("#town").append('<option value="'+d.id+'">'+d.name+'</option>');
                    });
                    form.render('select'); //刷新select选择框渲染
                }
            }
        });
    });
    //镇选择回调
    form.on('select(town)', function(data){
        var name = $(data.elem).find("option:selected").text();
        $("#townName").val(name);
        $("#townCode").val(data.value);
        $.ajax({
            url:"/village?pId="+data.value,
            type:'get',
            dataType:'json',
            success:function (rs) {
                if(rs){
                    $("#village").empty().append('<option value="">请选择社区(村)</option>');
                    $.each(rs,function(i,d){
                        $("#village").append('<option value="'+d.id+'">'+d.name+'</option>');
                    });
                    form.render('select'); //刷新select选择框渲染
                }
            }
        });
    });


    //村选择回调
    form.on('select(village)', function(data){
        var name = $(data.elem).find("option:selected").text();
        $("#villageName").val(name);
        $("#villageCode").val(data.value);
    });

    $("#baiduBtn").on('click',function () {
       var city= $("#city option:selected").text();
       var area= $("#area option:selected").text();
       var town=  $("#town option:selected").text();
       var village=$("#village option:selected").text();
       var xaddress=city+area+town;
        layer.open({
            title:"获取位置经纬度",
            content:"/static/baidu.html?a="+xaddress,
            area: ['650px', '600px'],
            btn: ['确定'],
            type:2,
            yes: function(index, layero){
                var poi = window[layero.find('iframe')[0]['name']].poi;
                $("#lng").val(poi.point.lng);
                $("#lat").val(poi.point.lat);
                layer.close(index);
            }
        });
    })

    // 上传负责人资质
    upload.render({
        elem: '#fzr'
        ,url: '/storage/files/ul'
        ,field: 'file'
        ,accept:"images"
        ,acceptMime:"image/jpg,image/jpeg,image/png"
        ,size:5120
        ,done: function(res){
            if(res.code != 200){
                return layer.msg('上传失败');
            }

            $.ajax({
                url:"/cert/save",
                type:'POST',
                dataType:'JSON',
                data:{name:res.data.fileName,path:res.data.path,type:8},
                success:function(rs){
                    if(rs.code == 200){
                        $("#fzr").before('<div class="layui-upload-list" style="margin:0 10px 5px;">' +
                            '<img class="layui-upload-img" src="/storage/images/'+rs.data.path+'_120x120"/>' +
                            '<p class="file-nme" title="'+rs.data.name+'">'+ rs.data.name +'</p>' +
                            '<p class="msg-box"><a target="_blank" href="/storage/files/'+res.data.path+'" class="layui-btn layui-btn-normal layui-btn-xs">查看</a><a  href="/storage/files/dl/'+res.data.path+'" class="layui-btn layui-btn-normal  layui-btn-xs">下载</a><a  href="/organization/delzshu?id='+rs.data.id+'" class="layui-btn layui-btn-normal  layui-btn-xs">删除</a></p></div>');

                    }else {
                        layer.msg('上传失败');
                    }
                }
            });

        }
    });
    // 上传机构照片
    upload.render({
        elem: '#jgzp'
        ,url: '/storage/files/ul'
        ,field: 'file'
        ,accept:"images"
        ,acceptMime:"image/jpg,image/jpeg,image/png"
        ,size:5120
        ,done: function(res){
            if(res.code != 200){
                return layer.msg('上传失败');
            }
            $.ajax({
                url:"/cert/save",
                type:'POST',
                dataType:'JSON',
                data:{name:res.data.fileName,path:res.data.path,type:9},
                success:function(rs){
                    if(rs.code == 200){
                        $("#jgzp").before('<div class="layui-upload-list" style="margin:0 10px 5px;">' +
                            '<img class="layui-upload-img" src="/storage/images/'+rs.data.path+'_120x120"/>' +
                            '<p class="file-nme" title="'+rs.data.name+'">'+ rs.data.name +'</p>' +
                            '<p class="msg-box"><a target="_blank" href="/storage/files/'+res.data.path+'" class="layui-btn layui-btn-normal layui-btn-xs">查看</a><a  href="/storage/files/dl/'+res.data.path+'" class="layui-btn layui-btn-normal  layui-btn-xs">下载</a><a  href="/organization/delzshu?id='+rs.data.id+'" class="layui-btn layui-btn-normal  layui-btn-xs">删除</a></p></div>');

                    }else {
                        layer.msg('上传失败');
                    }
                }
            });

        }
    });
});